package com.game.executor;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.game.config.NodeConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MarsWorkExecutor {
    @Autowired
    private NodeConfig nodeConfig;

    private Map<Integer, ExecutorService> executorServiceMap = new ConcurrentHashMap<>();

    public void submit(int id, Runnable work) {
        getExecutorService(id).submit(work);
    }

    private ExecutorService getExecutorService(int id) {
        int key = id % this.nodeConfig.workPoolNum;
        return this.executorServiceMap.computeIfAbsent(Integer.valueOf(key), k -> Executors.newSingleThreadExecutor());
    }
}
